//
//  ViewController.m
//  12-7bookshelf
//
//  Created by blue on 15-12-7.
//  Copyright (c) 2015年 hyd. All rights reserved.
//

#import "ViewController.h"
#import "readViewController.h"

@interface ViewController ()
{

    NSArray * imags;
    NSArray * texts;

}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"我的书架";
    self.tableView.showsHorizontalScrollIndicator = NO;//隐藏滚动条
    NSBundle * bundle = [NSBundle mainBundle];
    imags = [bundle URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    texts = [bundle URLsForResourcesWithExtension:@"txt" subdirectory:nil];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [ self.view addSubview:self.tableView];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!imags||imags.count ==0) {
        return 0;
    }
    NSInteger count = imags.count-1;
    
    return count/3 + 1;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return 162;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    NSMutableArray * arr =  [NSMutableArray array];
//    for (NSURL * url in imags) {
//        [arr addObject:[UIImage imageNamed:url.lastPathComponent]];
//        
//        
//        
//    }
//    static NSString * reuse =@"Reuse";
//    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    
//    if (!cell) {
//
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];

    NSInteger row = indexPath.row;
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    NSInteger index = row * 3;
    for (int i=0; i<3 && (index+1) < imags.count; i++) {
        UIImage * imag = [UIImage imageNamed:[imags[index +i]lastPathComponent]];
        UIImageView * iv = [[UIImageView alloc] initWithImage:imag];
        CGFloat edge = 12;
        CGFloat winth = (self.view.frame.size.width - edge*4)/3;
        iv.frame = CGRectMake(edge + (winth + edge)*i, 2, winth, 139-1-1*1);
        [cell addSubview:iv];
        
        UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom]];
        btn.frame = iv.bounds;
        btn.tag = 100 + index +i;
        [btn addTarget:self action:@selector(onClock:) forControlEvents:UIControlEventTouchUpInside];
        [iv addSubview:btn];
        iv.userInteractionEnabled = YES;
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1@2x.png"]];

    return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    readViewController * con = [[readViewController alloc]initWithStyle:UITableViewStyleGrouped];
    
    [self.navigationController pushViewController:con animated:YES];
    
}

-(void)onClock:(UIButton *)sender
{
    NSInteger index = sender.tag - 100;
    NSURL * imagURL = imags[index];
    NSString *FileName = imagURL.lastPathComponent;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
